package com.exam.controller;

import org.springframework.web.bind.annotation.*;
import com.exam.serviceimpl.BigModelNew;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;


@CrossOrigin(origins = "http://localhost:8088")

@RestController
@RequestMapping("/api/issue")
public class IssueController {

    @PostMapping("/showquestion")
    public Response showQuestion(@RequestBody Request request) {
        String subject = request.getSubject();
        String questionType = request.getQuestionType();

        // TODO: 通过 subject 和 questionType 生成或获取相应的问题
        String question = generateQuestion(subject, questionType);

        if (question != null) {
            return new Response(200, "问题已生成", question);
        } else {
            return new Response(500, "生成问题失败", null);
        }
    }

    @PostMapping("/showanswer")
    public Response showAnswer(@RequestBody Request request) {
        String question = request.getQuestion();

        // TODO: 根据问题获取答案
        String answer = generateAnswer(question);

        if (answer != null) {
            return new Response(200, "答案已生成", answer);
        } else {
            return new Response(500, "获取答案失败", null);
        }
    }
        //TODO:根据学科生成问题
    private String generateQuestion(String subject, String questionType) {
        try {
            //bigModelNew = new BigModelNew("userId", false);
            BigModelNew.flag=false;


            BigModelNew.showQuestion(subject,questionType);

            while (true)
            {
                if(BigModelNew.flag==true)
                {
                    break;

                }

            }
            String question = BigModelNew.totalAnswer; // 假设这是从大模型获取的答案
            return question;

        } catch (Exception e) {
            // 异常处理
            return null;
        }

    }
    //TODO:根据问题生成答案
    private String generateAnswer(String question) {
        try {
            //bigModelNew = new BigModelNew("userId", false);
            BigModelNew.flag=false;


            BigModelNew.showAnswer(question);

            while (true)
            {
                if(BigModelNew.flag==true)
                {
                    break;

                }

            }
            String answer = BigModelNew.totalAnswer; // 假设这是从大模型获取的答案
            return answer;

        } catch (Exception e) {
            // 异常处理
            return null;
        }
    }
}

class Request {
    private String subject;
    private String questionType;
    private String question;

    // getters and setters
    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getQuestionType() {
        return questionType;
    }

    public void setQuestionType(String questionType) {
        this.questionType = questionType;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }
}

class Response {
    private int code;
    private String message;
    private String data;

    // constructor
    public Response(int code, String message, String data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // getters and setters
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
